﻿using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ByteSite.WinClient.Common
{
    public class DateTimeExtensions
    {
    }

    /// <summary>
    /// DateTime Json序列化格式：yyyy-MM-dd HH:mm:ss
    /// </summary>
    public class TcFullDateTimeConverter : DateTimeConverterBase
    {
        private static IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return dtConverter.ReadJson(reader, objectType, Utils.ObjectToDateTime(existingValue), serializer);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            dtConverter.WriteJson(writer, Utils.ObjectToDateTime(value), serializer);
        }
    }

    /// <summary>
    /// DateTime Json序列化格式：yyyy-MM-dd HH:mm:ss
    /// </summary>
    public class TcDateTimeConverter : DateTimeConverterBase
    {
        private static IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd HH:mm:ss" };

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return dtConverter.ReadJson(reader, objectType, Utils.ObjectToDateTime(existingValue), serializer);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            dtConverter.WriteJson(writer, Utils.ObjectToDateTime(value), serializer);
        }
    }

    /// <summary>
    /// DateTime Json序列化格式：yyyy-MM-dd
    /// </summary>
    public class TcDateConverter : DateTimeConverterBase
    {
        private static IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "yyyy-MM-dd" };

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return dtConverter.ReadJson(reader, objectType, Utils.ObjectToDateTime(existingValue), serializer);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            dtConverter.WriteJson(writer, Utils.ObjectToDateTime(value), serializer);
        }
    }

    /// <summary>
    /// DateTime Json序列化格式：HH:mm:ss
    /// </summary>
    public class TcTimeConverter : DateTimeConverterBase
    {
        private static IsoDateTimeConverter dtConverter = new IsoDateTimeConverter { DateTimeFormat = "HH:mm:ss" };

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            return dtConverter.ReadJson(reader, objectType, Utils.ObjectToDateTime(existingValue), serializer);
        }

        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            dtConverter.WriteJson(writer, Utils.ObjectToDateTime(value), serializer);
        }
    }
}